﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.mission;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;

namespace Pixysoft.Weblications.Sango.Protocol
{
    class MissionFacade
    {
        public static List<mission_main> ToMissions(List<Entities.Usr_Mainmission> orms)
        {
            List<mission_main> list = new List<mission_main>();
            foreach (Entities.Usr_Mainmission orm in orms)
            {
                Entities.Mis_Mainmission sys = MissionHelper.getMainMission(orm.Missionserialcode);

                mission_main body = ReflectionManager.CreatePojo<mission_main>();
                list.Add(body);

                body.serialcode = orm.Missionserialcode;
                body.primarycode = orm.Primarycode;
                body.level = sys.Prizelevel;
                body.done = orm.Missionstatus == GeneralBillStatusCollection.FINISH;
                body.cash = sys.Prizevalue;
                body.content = sys.Description;
                body.title = sys.Title;
            }

            return list;
        }

        public static List<mission_daily> ToNewMissions(Entities.Usr_Profile profile, Entities.Usr_Mission orm, long buildingLevel)
        {
            List<mission_daily> list = new List<mission_daily>();

            foreach (DailyMissionItem item in MissionDao.getDailyMissions(orm))
            {
                if (item.Status != GeneralBillStatusCollection.NEW)
                    continue;

                list.Add(toDailyMission(profile, item, buildingLevel));
            }

            return list;
        }

        public static List<mission_daily> ToAcceptMissions(Entities.Usr_Profile profile, Entities.Usr_Mission orm, long buildingLevel)
        {
            List<mission_daily> list = new List<mission_daily>();

            foreach (DailyMissionItem item in MissionDao.getDailyMissions(orm))
            {
                if (item.Status == GeneralBillStatusCollection.NEW)
                    continue;

                list.Add(toDailyMission(profile, item, buildingLevel));
            }

            return list;
        }

        protected static List<mission_daily> ToMissions(Entities.Usr_Profile profile, Entities.Usr_Mission orm, long buildingLevel)
        {
            List<mission_daily> list = new List<mission_daily>();

            foreach (DailyMissionItem item in MissionDao.getDailyMissions(orm))
            {
                list.Add(toDailyMission(profile, item, buildingLevel));
            }

            return list;
        }

        private static mission_daily toDailyMission(Entities.Usr_Profile profile, DailyMissionItem item, long buildingLevel)
        {
            mission_daily body = ReflectionManager.CreatePojo<mission_daily>();

            Entities.Mis_Dailymission sys = MissionHelper.getDailyMissionByTypeCode(item.MissionTypeCode, item.Level);
            body.content = sys.Description;
            body.done = item.Status == GeneralBillStatusCollection.FINISH;
            body.processing = item.Status == GeneralBillStatusCollection.ACCEPT;
            body.eqmserialcode = sys.Equipmentserialcode;
            if (sys.Equipmentserialcode != PlayerFormular.default_number_empty)
                body.eqmname = EqmEquipmentHelper.searchBySerialcode(sys.Equipmentserialcode).Equipmentname;
            body.level = item.Level;
            body.soul = MissionFormular.getMissionDailySoulValue(profile.Userlevel, item.MissionTypeCode, item.Level, buildingLevel);
            body.title = sys.Title;
            body.primarycode = item.ItemCode;
            body.finishcount = item.FinishCount;
            return body;
        }
    }
}
